﻿import kliment.display.MCC;
import kliment.display.SpriteBtn;
import kliment.geom.BoundsRectangle;
/**
 * ...
 * @author	Kliment
 * @version	0.1.0
 */
class kliment.effect.StickyMC {
	private var _target_mc:MovieClip;
	private var _button:SpriteBtn;
	private var _isMove = false;
	private var _startX:Number;
	private var _startY:Number;
	private var _centerX:Number;
	private var _centerY:Number;
	private var _x1:Number;
	private var _y1:Number;
	private var _x2:Number;
	private var _y2:Number;
	private var _dx:Number;
	private var _dy:Number;
	private var _dx2:Number;
	private var _dy2:Number;
	private var _halfDX:Number;
	private var _halfDY:Number;
	private var _z:Number;
	private var _decay:Number = 1.5;
	private var _tuchLip:Number = 6;
	
	public function StickyMC(target_mc:MovieClip, button:SpriteBtn) {
		_target_mc = target_mc;
		_button = button;
		
		_startX = _target_mc._x;
		_startY = _target_mc._y;
		_centerX = _startX + _target_mc._width * .5;
		_centerY = _startY + _target_mc._height * .5;
		_x2 = _startX;
		_y2 = _startY;
		
		_button.addListener(this);
	}

	private function onRollOver():Void {
		_isMove = true;
		_x1 = _startX;
		_y1 = _startY;
		_x2 = _centerX + (_target_mc._parent._xmouse-_centerX) / _tuchLip - _target_mc._width * .5;
		_y2 = _centerY + (_target_mc._parent._ymouse-_centerY) / _tuchLip - _target_mc._height * .5;
		_dx = _x2 - _x1;
		_dy = _y2 - _y1;
		_halfDX = _dx * .5;
		_halfDY = _dy * .5;
		_z = 1;
	}

	private function onRollOut():Void {
		_isMove = false;
		_target_mc._x = _startX;
		_target_mc._y = _startY;
		_x2 = _startX;
		_y2 = _startY;
		_dx = _x2 - _x1;
		_dy = _y2 - _y1;
		_halfDX = _dx * .5;
		_halfDY = _dy * .5;
	}

	private function onDragOut():Void {
		_isMove = false;
		_target_mc._x = _startX;
		_target_mc._y = _startY;
		_x2 = _startX;
		_y2 = _startY;
		_dx = _x2 - _x1;
		_dy = _y2 - _y1;
		_halfDX = _dx * .5;
		_halfDY = _dy * .5;
	}

	private function onMouseMove():Void {
		if (_isMove) {
			_x2 = _centerX + (_target_mc._parent._xmouse-_centerX) / _tuchLip - _target_mc._width * .5;
			_y2 = _centerY + (_target_mc._parent._ymouse-_centerY) / _tuchLip - _target_mc._height * .5;
			_dx = _x2 - _x1;
			_dy = _y2 - _y1;
			_halfDX = (_halfDX + _dx) * .5;
			_halfDY = (_halfDX + _dy) * .5;
			_x1 = _x2;
			_y1 = _y2;
		}
	}

	private function onEnterFrame():Void {
		_z = _z * -1;
		_halfDX /= _decay * _z;
		_halfDY /= _decay * _z;
		_target_mc._x = _x2 + _halfDX;
		_target_mc._y = _y2 + _halfDY;
	}
	
	public function remove():Void {
		_button.removeListener(this);
	}
}